﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.AccessControl;

namespace Northwind.ServiceObjects
{
    [Guid("A0944FB6-DB29-410E-B5BD-43A15048E9C1")]
    [SupportedServiceApplication(NorthwindApplication.SupportedApplicationClassId, "1.0.0.0", typeof(NorthwindApplicationProxy))]
    public sealed class NorthwindServiceProxy : SPIisWebServiceProxy, IServiceProxyAdministration
    {
        //REFACTOR: read from resource or config?
        public static string ServiceApplicationProxyDefaultTypeName = "Northwind Application";
        public static string ServiceApplicationProxyDefaultTypeDescription = "Manages a Northwind Service";
        public static string DefaultName
        {
            get { return "Northwind Service Proxy"; }
        }

        #region Constructors
 		public NorthwindServiceProxy()
		{
		}

		public NorthwindServiceProxy(SPFarm farm)
			: this(DefaultName, farm)
		{
		}

		public NorthwindServiceProxy(string name, SPFarm farm)
			: base(farm)
		{
			// NOTE: This constructor is required if this service proxy is installed by the 
			// command psconfig -cmd services -install.

			Name = name;
		}
       #endregion

        #region IServiceProxyAdministration Members
		// NOTE: The interface IServiceProxyAdministration supports connections to service 
		// applications in remote farms using the "Connect" experience in the ribbon on 
		// the service application management page.

		public SPServiceApplicationProxy CreateProxy(Type serviceApplicationProxyType, string name, Uri serviceApplicationUri, SPServiceProvisioningContext provisioningContext)
		{
			if(serviceApplicationProxyType != typeof(NorthwindApplicationProxy))
				throw new NotSupportedException();

			// Create a proxy for the specified (typically remote) service application
			return new NorthwindApplicationProxy(name, this, serviceApplicationUri);
		}

		public SPPersistedTypeDescription GetProxyTypeDescription(Type serviceApplicationProxyType)
		{
            if (serviceApplicationProxyType != typeof(NorthwindApplicationProxy))
            {
                throw new NotSupportedException();
            }

			return new SPPersistedTypeDescription(ServiceApplicationProxyDefaultTypeName, ServiceApplicationProxyDefaultTypeDescription);
		}

		public Type[] GetProxyTypes()
		{
			return new Type[] { typeof(NorthwindApplicationProxy) };
		}

        #endregion

        #region Overrides
        internal NorthwindApplicationProxyCollection ApplicationProxies
        {
            get
            {
                return new NorthwindApplicationProxyCollection(this);
            }
        }
        #endregion    
    }
}
